<template>
<div class="page-wrapper">
  <Breadcrumb v-bind="breads" />
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-body">
            <form class="form-inline" role="form" @submit.prevent="searchPlatRoles(1)">
              <div class="form-group" v-show="!isLogin">
                <label class="control-label">{{platName()}}名称：</label>
                <div class="btn-group">
                  <input type="text" :name="platType +'.name'" @change="setSearch" class="form-control" style="width:150px;">
                </div>
              </div>
              <div class="form-group" v-show="!isLogin">
                <label class="control-label">{{platName()}}号：</label>
                <div class="btn-group">
                  <input type="text" :name="platType +'.' + platType + 'No'" @change="setSearch" class="form-control" style="width:150px;">
                </div>
              </div>
              <div class="form-group">
                <label class="control-label">角色名称：</label>
                <div class="btn-group">
                  <input type="text" name="role.name" @change="setSearch" class="form-control" style="width:150px;">
                </div>
              </div>
              <div class="form-group">
                <label class="control-label">角色编码：</label>
                <div class="btn-group">
                  <input type="text" name="role.code" @change="setSearch" class="form-control" style="width:150px;">
                </div>
              </div>
              <div class="form-group">
                <label class="control-label">&nbsp;</label>
                <button type="submit" class="btn btn-primary">查 找</button>
              </div>
              <div class="form-group">
                <label class="control-label">&nbsp;</label>
                <div class="btn-group">
                  <button type="button" class="btn btn-primary" v-on:click="newPlatRole()">新 增</button>
                </div>
              </div>
            </form>
          </div>
          <div class="col-12 table-responsive">
            <table class="table table-hover">
              <template v-if="!isLogin">
              <col width="20%">
              <col width="20%">
              <col width="20%">
              <col width="20%">
              <col width="20%">
              </template>
              <template v-else>
              <col width="33%">
              <col width="33%">
              <col width="33%">
              </template>
              <thead>
                <tr>
                  <th v-if="!isLogin">{{platName()}}</th>
                  <th v-if="!isLogin">{{platName()}}号</th>
                  <th>角色名称</th>
                  <th>角色编码</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="(platRole, index) in platRoles" :key="platRole.id.roleId">
                  <td v-if="!isLogin">{{platNameText(platRole)}}</td>
                  <td v-if="!isLogin">{{platNoText(platRole)}}</td>
                  <td>{{platRole.role.name}}</td>
                  <td>{{platRole.role.code}}</td>
                  <td>
                    <a :href="`#/role-permit-${platType}-${platRole.role.roleId}`" class="badge badge-info" title="权限">
                      <span class="far fa-user"> 权限</span>
                    </a>
                    &nbsp;
                    <a href="javascript:void(0);" class="badge badge-info" title="编辑" v-on:click="editPlatRole(index)">
                      <span class="far fa-edit"> 编辑</span>
                    </a>
                    &nbsp;
                    <a href="javascript:void(0);" class="badge badge-info" title="删除" v-on:click="delPlatRole(index)">
                      <span class="far fa-trash-alt"> 删除</span>
                    </a>
                    &nbsp;
                  </td>
                </tr>
              </tbody>
            </table>
            <div class="col-12">
              <PageLink v-bind="paging" v-on:go-page="searchPlatRoles($event)" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <Footer />
  <RoleModal ref="roleModal" :platType="platType" v-on:role="setRole($event)" />
  <!--<PlatModal ref="platModal" v-on:plat="setPlat($event)" />-->
  <PlatModal ref="platModal" :platType="platType" v-on:plat="setPlat($event)" />

</div>
</template>
<script>
  import $ from 'jquery'
  import cfg from '../dist/js/config'

  import Footer from './ComFooter'
  import Breadcrumb from './ComBreadcrumb'
  import PageLink from './ComPageLink'
  import RoleModal from './RoleEditModal'
  import PlatModal from './PlatFindModal'

  import { platforms } from '../dist/js/enum'

  // 查询条件
  const search = {}

  // 列表数据
  const platRoles = []

  // 分页信息
  const paging = {}

  // 添加/编辑对象
  const platRole = {}

  var loginPlat = null

  function loadLoginPlat() {
    this.isLogin = true
    $.get(cfg.serverPath + '/login/' + this.platType, result => {
      if (result.status != 0) {
        $(document).failMsg(result.message)
        return
      }
      if (result.data) {
        loginPlat = result.data
        this.isLogin = true
      } else {
        this.isLogin = false
      }
      this.searchPlatRoles()
    })
  }
  function searchPlatRoles(nowPage) {
    if (nowPage) {
      this.search.nowPage = nowPage
    }
    if (this.isLogin) {
      this.search[this.platType + '.' + this.platType + 'Id'] = loginPlat[this.platType + 'Id']
    }
    $.get(cfg.serverPath + '/' + this.platType + 'Role/page/' + (this.isLogin ? 'role' : (this.platType + 'AndRole')),
      this.search,
      result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        Object.assign(this.paging, this.paging, result.data.paging)
        this.platRoles.splice(0, this.platRoles.length)
        if (!result.data.list) {
          return
        }
        for (let platRole of result.data.list) {
          this.platRoles.push(platRole) }
      })
  }
  function newPlatRole() {
    if (!this.isLogin) {
      this.$refs.platModal.open()
    } else {
      this.$refs.roleModal.setRole({
        platform: this.platformValue()
      })
      this.$refs.roleModal.open()
    }
  }
  function editPlatRole(index) {
    this.platRole = this.platRoles[index]
    this.$refs.roleModal.setRole(this.platRole.role)
    this.$refs.roleModal.open()
  }
  function savePlatRole() {
    $.ajax({
      contentType: 'application/json; charset=UTF-8',
      url: cfg.serverPath + '/' + this.platType + 'Role/add',
      type: 'put',
      data: JSON.stringify(this.platRole),
      success: result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        let isExists = false
        for (let i in this.platRoles) {
          let exists = this.platRoles[i]
          if ((exists.id)[this.platType + 'Id'] == (this.platRole.id)[this.platType + 'Id']
            && exists.id.roleId == this.platRole.id.roleId) {
            this.platRoles.splice(i, 1, this.platRole)
            isExists = true
            break;
          }
        }

        if (isExists) {
          $(document).infoMsg('更新成功')
          return
        }

        $(document).infoMsg('添加成功')
        this.platRoles.unshift(this.platRole)
      }
    })
  }
  function delPlatRole(index) {
    $.ajax({
      contentType: 'application/json; charset=UTF-8',
      url: cfg.serverPath + '/' + this.platType + 'Role/del',
      type: 'delete',
      data: JSON.stringify(this.platRoles[index].id),
      success: result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        $(document).infoMsg('删除成功')
        this.platRoles.splice(index, 1)
      }
    })
  }
  function setPlat(plat) {
    this.platRole = {
      id: {}
    }
    this.$set(this.platRole.id, this.platType + 'Id', plat[this.platType + 'Id'])
    this.$set(this.platRole, this.platType, plat)
    this.$refs.roleModal.setRole({})
    this.$refs.roleModal.open()

  }
  function setRole(role) {
    if (this.isLogin) {
      this.platRole = {
        id: {}
      }
      this.$set(this.platRole.id, this.platType + 'Id', loginPlat[this.platType + 'Id'])
      this.$set(this.platRole, this.platType, loginPlat)
    }
    this.platRole.id.roleId = role.roleId
    this.platRole.role = role
    this.savePlatRole()
  }
  function setSearch(event) {
    this.search[event.target.name] = event.target.value
  }
  function reset() {
    this.search = {
      'sorts[0].field': 'id.roleId',
      'sorts[0].isDesc': true,
      nowPage: 1
    }
    this.platRoles = []
    this.paging = {}
    this.platRole = {}
    loginPlat = null
    this.loadLoginPlat()
  }

  function breads() {
    return {
      title: '角色管理',
      items: [
        {
          href: '#/',
          name: '主页'
        },
        {
          href: '',
          name: '角色管理'
        }
      ]
    }
  }
  function platName() {
    if (this.platType == 'agent') {
      return '服务商'
    } else if (this.platType == 'merch') {
      return '商户'
    } else if (this.platType == 'shop') {
      return '门店'
    } else if (this.platType == 'user') {
      return '客户'
    }
  }
  function platNameText(platAdmin) {
    return platAdmin[this.platType + ''].name
  }
  function platNoText(platAdmin) {
    return platAdmin[this.platType + ''][this.platType + 'No']
  }
  function platformValue() {
    for (let i in this.platforms) {
      if (this.platforms[i].name.toLowerCase().indexOf(this.platType) != -1) {
        return i
      }
    }
  }
  export default {
    props: {
      platType: String
    },
    components: {
      Breadcrumb,
      Footer,
      PageLink,
      PlatModal,
      RoleModal
    },
    data() {
      return {
        platforms,
        search,
        platRole,
        platRoles,
        paging,
        isLogin: true
      }
    },
    methods: {
      reset,
      loadLoginPlat,
      searchPlatRoles,
      newPlatRole,
      editPlatRole,
      savePlatRole,
      delPlatRole,
      setPlat,
      setRole,
      setSearch,
      platName,
      platNameText,
      platNoText,
      platformValue
    },
    computed: {
      breads
    },
    created() {
      this.reset()
    },
    watch: {
      platType() {
        this.reset()
      }
    }
  }
</script>